<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	/**
	 * This class contains a number of non-common room management methods.
	 */
	class lib_room_extended {
		const E_NONE = 0;
		const E_NOT_FOUND = 1;
		const E_INVALID_TYPE = 2;
		const E_INVALID_PERM = 3;
		
		/**
		 * Get the error message associated with an error number
		 *
		 * @param int $error_code The error number
		 * @return The error message associated with that error message
		 */
		public function get_error_message($error_code){
			switch($error_code){
				case lib_room_extended::E_NOT_FOUND:
					return $GLOBALS['LANG']->lang_messages->user_or_group_not_found;
				break;
				case lib_room_extended::E_INVALID_TYPE:
					return $GLOBALS['LANG']->lang_messages->invalid_type;
				break;
				case lib_room_extended::E_INVALID_PERM:
					return $GLOBALS['LANG']->lang_messages->invalid_permissions;
				break;
				default:
					return '';
				break;
			}
		}
		
		/**
		 * Sets abstract room permissions for a specific user or group.
		 *
		 * @param lib_room $room The room to make the modifications to
		 * @param Object $object The lib_group or lib_user object to grant/deny permissions to
		 * @param string $type Can be either 'user' or 'group', should represent the type of $object
		 * @param string $permission The action to do, may be moderator, voice, devoice, mute, unmute, kick, unban
		 * @param boolean $override If true bypassed any failed permissions checks
		 * @return True if the permissions changes were successfully made, otherwise an error string is returned.
		 * 		The possible error strings are invalidtype, user_not_found, invalidperm
		 */
		static public function set_permissions_mode($room,$object,$type,$permission,$override=false){
			global $ses;
			
			if($type == 'user'){
				$object = new lib_user($object);
				$id_field = 'uid';
			}elseif($type == 'group'){
				$object = new lib_group($object);
				$id_field = 'gid';
			}else
				return lib_room_extended::E_INVALID_TYPE;
				
			$GLOBALS['last_object'] =& $object;
			
			if(!$object->is_valid())
				return lib_room_extended::E_NOT_FOUND;
			
			$type .= '_perms';
			
			if(!isset($room->data_cache[$type][$object->data_cache[$id_field]]))
				$room->data_cache[$type][$object->data_cache[$id_field]] = array('allow'=>0,'deny'=>0);
			
			switch($permission){
				case 'moderator':
					if(!$override && !($room->has_permission($ses->user,'MUTE_USER'))) return 'access_denied';
					$room->set_permission('SEND_REGULAR_MESSAGE',1,$object);
					$room->set_permission('READ_UNVOICED_USERS',1,$object);
				break;
				case 'devoice':
					if(!$override && !($room->has_permission($ses->user,'MUTE_USER'))) return 'access_denied';
					$room->set_permission('SEND_REGULAR_MESSAGE',0,$object);
					$room->set_permission('READ_UNVOICED_USERS',0,$object);
				break;
				case 'voice':
					if(!$override && !($room->has_permission($ses->user,'MUTE_USER'))) return 'access_denied';
					$room->set_permission('SEND_REGULAR_MESSAGE',1,$object);
					$room->set_permission('READ_UNVOICED_USERS',0,$object);
				break;
				case 'mute':
					if(!$override && !($room->has_permission($ses->user,'MUTE_USER'))) return 'access_denied';
					$room->set_permission('SEND_REGULAR_MESSAGE',-1,$object);
				break;
				case 'unmute':
					if(!$override && !($room->has_permission($ses->user,'MUTE_USER'))) return 'access_denied';
					$room->set_permission('SEND_REGULAR_MESSAGE',0,$object);
				break;
				case 'kick':
					if(!$override && ($type == 'group' || !$room->has_permission($ses->user,'BAN_KICK_USER') || $room->has_permission($object,'BAN_IMMUNITY'))) return 'access_denied';
					$room->set_permission('ACCESS_ROOM',-1,$object);
					lib_sending::send($room,null,str_replace('_u',$object->data_cache['name'],$GLOBALS['LANG']->lang_global->user_kicked),false,true);
					$room->part($object);
				break;
				case 'unban':
					if(!$override && ($type == 'group' || !$room->has_permission($ses->user,'BAN_KICK_USER'))) return 'access_denied';
					if(isset($room->data_cache['ban_data']['u'.$object->data_cache[$id_field]]))
						unset($room->data_cache['ban_data']['u'.$object->data_cache[$id_field]]);
					$room->set_permission('ACCESS_ROOM',0,$object);
				break;
				default:
					return lib_room_extended::E_INVALID_PERM;
				break;
			}
			
			$room->commit_changes();
			return lib_room_extended::E_NONE;
		}
		
		static public function ban_user($room,$uid,$length){
			global $ses;
			
			if(($error = lib_room_extended::set_permissions_mode($room,$uid,'user','kick',false)) === true){
				if($length != 0) $length = $ses->time + $length;
				
				$room->data_cache['ban_data']['u'.$GLOBALS['last_object']->data_cache['uid']] = $length;
				$room->commit_changes();
			}
			
			return $error;
		}
		
	}
?>